package com.itcoon.cloud.framework.mybatis.typehandler;

import com.itcoon.cloud.framework.enums.ValueEnum;

import java.io.Serializable;
import java.util.Collection;

public abstract class AbstractValueEnumCollectionToSegmentsTypeHandler<V extends Serializable, E extends Enum<E>& ValueEnum<V, E>> extends AbstractCollectionToSegmentsTypeHandler<E> {

    private final Class<E> enumType;

    protected AbstractValueEnumCollectionToSegmentsTypeHandler(Class<E> enumType, Class<? extends Collection> collectionType) {
        super(collectionType);
        this.enumType = enumType;
    }

    @Override
    public String convertSingle(E e) {
        return String.valueOf(e.value());
    }

    @Override
    public E reverseConvertSingle(String item) {
        return ValueEnum.fromValue(reverseValue(item), enumType);
    }

    abstract V reverseValue(String item);
}
